package com.djmw.mouse.entity;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.Transient;

@Entity
@Table(name = "t_cv")
public class Cv extends IdEntity {
    public static final int STATUS_ENABLED = 0;
    public static final int STATUS_DISABLED = 1;
    public static final int STATUS_DELETED = 10;

    private MemberUser memberUser;
    private String name;
    private List<CvLang> cvLang;
    private int status;
    private int curLang = CvLang.LANG_CHINESE;

    @ManyToOne
    @JoinColumn(name = "c_member_id")
    public MemberUser getMemberUser() {
        return memberUser;
    }

    public void setMemberUser(MemberUser member) {
        this.memberUser = member;
    }

    @Column(name = "c_name")
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "cv")
    @OrderBy("lang ASC")
    public List<CvLang> getCvLang() {
        return cvLang;
    }

    public void setCvLang(List<CvLang> cvLang) {
        this.cvLang = cvLang;
    }

    @Column(name = "c_status")
    public int getStatus() {
        return status;
    }
    public void setStatus(int status) {
        this.status = status;
    }

    @Transient
    public int getCurLang() {
        return curLang;
    }

    public void setCurLang(int curLang) {
        this.curLang = curLang;
    }

    @Transient
    public CvLang getCurCvLang() {
        if (getCvLang() == null) {
            setCvLang(new ArrayList<CvLang>());
        }
        for (CvLang cvl : getCvLang()) {
            if (cvl.getLang() == curLang) {
                return cvl;
            }
        }
        return null;
        // CvLang curCvLang = new CvLang(this, curLang);
        // getCvLang().add(curCvLang);
        // return curCvLang;
    }

    public void setCurCvLang(CvLang curCvLang) {
        if (getCvLang() == null) {
            setCvLang(new ArrayList<CvLang>());
        }
        if (curCvLang.getLang() == null) {
            curCvLang.setLang(curLang);
        }
        if (curCvLang.getCv() == null) {
            curCvLang.setCv(this);
        }
        for (int i = 0; i < getCvLang().size(); i++) {
            if (getCvLang().get(i).getLang() == curLang) {
                getCvLang().set(i, curCvLang);
                return;
            }
        }
        getCvLang().add(curCvLang);
    }
}
